<?php
if(!defined('BASEPATH'))
    exit('No direct script access allowed');

class Sourcecode extends CI_Controller {
    public function __construct() {
        parent::__construct();

        $this -> load -> helper('url');
        $this -> load -> helper('form');
    }

    public function _loadviews() {
        $menu['items'] = array("/sourcecode/index" => "New Sourcecode", "/sourcecode/listall" => "See all");

        $this -> load -> view('shared/header', $menu);
        $this -> load -> view('shared/footer');
    }

    public function _generatekey() {
        $num_chars = 5;
        //max length of random chars
        $i = 0;
        $my_keys = "123456789abcdefghijklmnopqrstuvwxyz";
        //keys to be chosen from
        $keys_length = strlen($my_keys);
        $url = "";
        while($i < $num_chars) {
            $rand_num = mt_rand(1, $keys_length - 1);
            $url .= $my_keys[$rand_num];
            $i++;
        }
        return $url;
    }

    public function _today() {
        return date("m/d/y g:i A");
    }

    public function index() {
        $this -> _loadviews();
        $data['key'] = $this -> _generatekey();
        $this -> load -> view('sourcecode/index', $data);

    }

    public function save() {

        $data = array('source_code' => $this -> input -> post('source_code'), 'source_unique' => $this -> input -> post('source_unique'));

        $this -> db -> insert('tblsourcecode', $data);

        redirect('sourcecode/share/' . $this -> db -> insert_id());
    }

    public function share($key = '') {
        $this -> load -> helper('form');
        $this -> load -> helper('url');

        $data = array();
        if(isset($key)) {
            $this -> db -> where('source_unique', $key);
            $this -> db -> or_where('source_id', $key);
            $data['query'] = $this -> db -> get('tblsourcecode');
            //$data['query'] = $this -> db -> get_where('tblsourcecode', array('source_unique' => $key, 'source_id' => $key));
        }

        $this -> _loadviews();
        $this -> load -> view('sourcecode/share', $data);
    }

    public function listall() {
        $this -> load -> helper('form');
        $this -> load -> helper('url');

        $data['query'] = $this -> db -> get('tblsourcecode');

        $this -> _loadviews();
        $this -> load -> view('sourcecode/share', $data);
    }

}

/* End of file sourcecode.php */
/* Location: ./application/controllers/sourcecode.php */
